#ifndef STLKERNEL_H
#define STLKERNEL_H

#include <algorithm>

#include "../../QueryDataManipulation/src/QueryDataManipulation.hpp"

template<typename DataIterator, typename SelectType = DataIterator> class StlKernel : public QDM::IKernel<DataIterator, SelectType>
{
public:
	virtual void where(ResultVector& resultVector, WhereFunction isRequired) const
	{
		auto isNeedRemove = [&](DataIterator item){return !isRequired(item);};
		resultVector.erase(std::remove_if(resultVector.begin(), resultVector.end(), isNeedRemove), resultVector.end());
	}

	virtual void orderBy(ResultVector& resultVector, OrderByFunction isNeedChange) const
	{
		std::sort(resultVector.begin(), resultVector.end(), isNeedChange);

		DataIterator buffer;
		size_t size = resultVector.size();

		for (size_t i = 0; i < size / 2; i++)
		{
			buffer = resultVector[i];
			resultVector[i] = resultVector[size - 1 - i];
			resultVector[size - 1 - i] = buffer;
		}
	}

	virtual void select(const ResultVector& resultVector, SelectVector& resultSelect, SelectFunction selectFunction) const
	{
		resultSelect.resize(resultVector.size());
		std::transform(resultVector.begin(), resultVector.end(), resultSelect.begin(), selectFunction);
	}

	virtual void execute(ResultVector& resultVector, ExecuteFunction executeFunction) const
	{
		executeFunction(resultVector, -1);
	}
};

#endif